package com.google.gac.masai.getit;

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

//the initialization for NumSpecRules is here

public class MenuArrayAdapter extends ArrayAdapter<String> {

	private final Activity context;
	private final String[] names;
	
	private SharedPreferences mPreferences;

	public MenuArrayAdapter(Activity context, String[] names) {
		super(context, R.layout.rowlayout, names);
		this.context = context;
		this.names = names;
		
		mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        
        SharedPreferences.Editor edit = mPreferences.edit();
        edit.putInt("NumSpecRules", 0);
        edit.putBoolean("first_special", false);
        edit.putBoolean("second_special", false);
        edit.putBoolean("third_special", false);
        		
		edit.commit();
	}

	// static to save the reference to the outer class and to avoid access to
	// any members of the containing class
	static class ViewHolder {
		public ImageView imageView;
		public TextView textView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// ViewHolder will buffer the assess to the individual fields of the row layout
		
		ViewHolder holder;
		// Recycle existing view if passed as parameter
		// This will save memory and time on Android
		// This only works if the base layout for all classes are the same
		View rowView = convertView;
		if(rowView == null){
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.rowlayout, null, true);
			holder = new ViewHolder();
			holder.textView = (TextView) rowView.findViewById(R.id.label);
			holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
			rowView.setTag(holder);
		}else{
			holder = (ViewHolder) rowView.getTag();
		}
		 
		holder.textView.setText(names[position]);
		// Change the icon for each line.
		switch (position) {
		case 0:
			Log.v("MenuArrayAdapter", "HELLO");
			holder.imageView.setImageResource(R.drawable.downloads);
			break;
		case 1:
			holder.imageView.setImageResource(R.drawable.file);
			break;
		case 2:
			holder.imageView.setImageResource(R.drawable.setting);
			break;
		default:
			break;
		}
		//Log.v("TEST", "" + position);
		
		
		return rowView;
	}
	
	

}
